#include "moneywindow.h"
#include "ui_moneywindow.h"

#include <QMessageBox>
#include <QStandardItemModel>

MoneyWindow::MoneyWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MoneyWindow)
{
    ui->setupUi(this);
    ad = new AddDialog(this);
    connect(ad, SIGNAL(UpdateComBox()), this, SLOT(onUpdateNameComBox()));
    // connect sqlite
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("money.db");
    if(!db.open())
    {
        qDebug() << "database error!";
    }
    onUpdateNameComBox();
    initTotalTable();
    initUserTable();
}

MoneyWindow::~MoneyWindow()
{
    ad->deleteLater();
    delete ui;
}

int MoneyWindow::countSql(QSqlQuery query)
{
    int count = 0;
    while(query.next())
    {
        count += 1;
    }
    query.first();
    return count;
}

void MoneyWindow::initTotalTable()
{
    QStandardItemModel *pModel = new QStandardItemModel;
    pModel->setColumnCount(2);
    pModel->setHeaderData(0, Qt::Horizontal, tr("姓名"));
    pModel->setHeaderData(1, Qt::Horizontal, tr("金额"));
    QSqlQuery query;
    QString qry_ct = "select * from user;";
    QByteArray ay_ct = qry_ct.toUtf8();
    query.exec(QObject::tr(ay_ct.data()));

    int count = 0;
    while(query.next())
    {
        pModel->setItem(count, 0, new QStandardItem(query.value(0).toString()));
        pModel->setItem(count, 1, new QStandardItem(query.value(1).toString()));
        count += 1;
    }
    ui->TotalTableView->setModel(pModel);
}

void MoneyWindow::initUserTable()
{
    QStandardItemModel *pModel = new QStandardItemModel;
    pModel->setColumnCount(3);
    pModel->setHeaderData(0, Qt::Horizontal, tr("姓名"));
    pModel->setHeaderData(1, Qt::Horizontal, tr("动作"));
    pModel->setHeaderData(2, Qt::Horizontal, tr("备注"));

    QString name = ui->userComboBox->currentText();
    QSqlQuery query;
    QString qry_ct = "select * from status where username='"+ name +"';";
    QByteArray ay_ct = qry_ct.toUtf8();
    query.exec(QObject::tr(ay_ct.data()));

    int count = 0;
    while(query.next())
    {
        pModel->setItem(count, 0, new QStandardItem(query.value(0).toString()));
        pModel->setItem(count, 1, new QStandardItem(query.value(1).toString()));
        pModel->setItem(count, 2, new QStandardItem(query.value(2).toString()));
        count += 1;
    }
    ui->UserTableView->setModel(pModel);
}

void MoneyWindow::on_actionExit_triggered()
{
    this->close();
}

void MoneyWindow::on_AddButton_clicked()
{
    QString name = ui->NameComboBox->currentText();
    QString money = "增加 " + ui->MoneyLineEdit->text();
    QString msg = ui->MsgLineEdit->text();

    QSqlQuery query;
    QString qry_ct = "select money from user where username='" + name +"';";
    QByteArray ay_ct = qry_ct.toUtf8();
    query.exec(QObject::tr(ay_ct.data()));
    if(countSql(query))
    {
        int intMoney = query.value(0).toInt();
        QString inStr = "insert into status (username,action,msg) values ('" + name + "','" + money + "','"+ msg +"');";
        qDebug() << inStr;
        QByteArray ay_is = inStr.toUtf8();
        query.exec(QObject::tr(ay_is.data()));
        QMessageBox msgBox;
        msgBox.setText("insert成功！");
        msgBox.exec();

        int upMoney = intMoney + ui->MoneyLineEdit->text().toInt();
        qDebug() << upMoney;
        QString upStr = "update user set money="+ QString::number(upMoney, 10) +" where username='" + name + "';";
        QByteArray upAy = upStr.toUtf8();
        query.exec(QObject::tr(upAy.data()));
        ui->moneyLabel->setText(QString::number(upMoney, 10));
    }
    else
    {
        QMessageBox msgBox;
        msgBox.setText("error");
        msgBox.exec();
    }
}

void MoneyWindow::on_SubtractButton_clicked()
{
    QString name = ui->NameComboBox->currentText();
    QString money = "减少 " + ui->MoneyLineEdit->text();
    QString msg = ui->MsgLineEdit->text();

    QSqlQuery query;
    QString qry_ct = "select money from user where username='" + name +"';";
    QByteArray ay_ct = qry_ct.toUtf8();
    query.exec(QObject::tr(ay_ct.data()));
    if(countSql(query))
    {
        int intMoney = query.value(0).toInt();
        QString inStr = "insert into status (username,action,msg) values ('" + name + "','" + money + "','"+ msg +"');";
        qDebug() << inStr;
        QByteArray ay_is = inStr.toUtf8();
        query.exec(QObject::tr(ay_is.data()));
        QMessageBox msgBox;
        msgBox.setText("insert成功！");
        msgBox.exec();

        int upMoney = intMoney - ui->MoneyLineEdit->text().toInt();
        qDebug() << upMoney;
        QString upStr = "update user set money="+ QString::number(upMoney, 10) +" where username='" + name + "';";
        QByteArray upAy = upStr.toUtf8();
        query.exec(QObject::tr(upAy.data()));
        ui->moneyLabel->setText(QString::number(upMoney, 10));
    }
    else
    {
        QMessageBox msgBox;
        msgBox.setText("error");
        msgBox.exec();
    }
}

void MoneyWindow::on_NameComboBox_activated(const QString &arg1)
{
    qDebug() << arg1;
    QSqlQuery query;
    QString qry_ct = "select * from user where username='"+ arg1 +"';";
    QByteArray ay_ct = qry_ct.toUtf8();
    query.exec(QObject::tr(ay_ct.data()));
    query.first();
    ui->moneyLabel->setText(query.value(1).toString());
}

void MoneyWindow::on_actionAdd_triggered()
{
    ad->show();
}

void MoneyWindow::onUpdateNameComBox()
{
    ui->NameComboBox->clear();
    QSqlQuery query;
    QString qry_ct = "select * from user;";
    QByteArray ay_ct = qry_ct.toUtf8();
    query.exec(QObject::tr(ay_ct.data()));

    while(query.next())
    {
//        qDebug() << query.value(0).toString();
        ui->NameComboBox->addItem(query.value(0).toString());
        ui->userComboBox->addItem(query.value(0).toString());
    }
    query.first();
    ui->moneyLabel->setText(query.value(1).toString());
}

void MoneyWindow::on_userComboBox_activated(const QString &arg1)
{

    QStandardItemModel *pModel = new QStandardItemModel;
    pModel->setColumnCount(3);
    pModel->setHeaderData(0, Qt::Horizontal, tr("姓名"));
    pModel->setHeaderData(1, Qt::Horizontal, tr("动作"));
    pModel->setHeaderData(2, Qt::Horizontal, tr("备注"));
    QSqlQuery query;
    QString qry_ct = "select * from status where username='"+ arg1 +"';";
    QByteArray ay_ct = qry_ct.toUtf8();
    query.exec(QObject::tr(ay_ct.data()));

    int count = 0;
    while(query.next())
    {
        pModel->setItem(count, 0, new QStandardItem(query.value(0).toString()));
        pModel->setItem(count, 1, new QStandardItem(query.value(1).toString()));
        pModel->setItem(count, 2, new QStandardItem(query.value(2).toString()));
        count += 1;
    }
    ui->UserTableView->setModel(pModel);
}

void MoneyWindow::on_tabWidget_tabBarClicked(int index)
{
    switch (index) {
    case 0:
        break;
    case 1:
        initTotalTable();
        break;
    case 2:
        initUserTable();
        break;
    default:
        break;
    }
}
